package com.landerer.math;

public final class ExpensiveCalculation {

	private final int a;
	private final int b;
	private int result;
	private boolean cached;

	private ExpensiveCalculation(final int a, final int b) {

		this.a = a;
		this.b = b;
	}

	public int getResult() {

		if (this.cached) {
			return this.result;
		} else {
			try {
				Thread.sleep(5000);
				this.cached = true;
				this.result = this.a + this.b;
				return this.result;
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public static ExpensiveCalculation valueOf(final int a, final int b) {

		return new ExpensiveCalculation(a, b);
	}
}
